package org.rc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.rc.dto.UserAddDto;
import org.rc.dto.UserPageDto;
import org.rc.dto.UserUpdateDto;
import org.rc.entity.User;
import org.rc.mapper.UserMapper;
import org.rc.service.UserService;
import org.rc.util.Md5Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Value("${user.default.pwd}")
    private String defaultPwd;

    @Override
    public User addUser(UserAddDto userAddDto) {
        User user = User.getInstance(userAddDto);
        user.setIsAdmin(2);
        user.setIsDeleted(1);
        user.setIsDcz(1);
        user.setPwd(Md5Util.encrypt(defaultPwd));
        this.save(user);
        return user;
    }

    @Override
    public void updateUserById(User user) {
        this.updateById(user);
    }

    @Override
    public void updateUser(UserUpdateDto userUpdateDto) {
        User user = this.getById(userUpdateDto.getId());
        user.setName(userUpdateDto.getName());
        user.setLoginName(userUpdateDto.getLoginName());
        user.setGender(userUpdateDto.getGender());
        user.setPhoneNo(userUpdateDto.getPhoneNo());
        user.setPost(userUpdateDto.getPost());
        this.updateById(user);
    }

    @Override
    public Page<User> getUserPage(UserPageDto userPageDto) {
        Page<User> page = new Page<>(userPageDto.getPageNo(),userPageDto.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(userPageDto.getName()),"name",userPageDto.getName())
                .ne("login_name","admin").eq("is_deleted",1).eq("is_dcz",userPageDto.getIsDcz());
        return this.baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public User login(String name, String pwd) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("login_name",name).eq("pwd",pwd);
        return this.getOne(qw);
    }

    @Override
    public User getByUserId(Long id) {
        return getById(id);
    }

    @Override
    public List<User> getSimilarAccountByLoginName(String loginName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight(StringUtils.isNotEmpty(loginName),"login_name",loginName);
        return list(queryWrapper);
    }

    @Override
    public void updatePwd(Long userId, String password) {
        QueryWrapper<User> qw = new QueryWrapper<User>().eq("id",userId);
        User user = this.getOne(qw);
        user.setPwd(password);
        this.updateById(user);
    }

    @Override
    public void deleteById(Long id) {
        this.removeById(id);
    }

    @Override
    public void batchDeleteUsers(List<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public List<User> getByUserIds(List<Long> userIds) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",userIds);
        return this.list(queryWrapper);
    }

    @Override
    public List<User> getAllDczUsers() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",1).eq("is_admin",2).eq("is_dcz",2);
        return this.list(queryWrapper);
    }

    @Override
    public List<User> getUsersByUserIds(List<Long> userIds) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",userIds).eq("is_deleted",1);
        return this.list(queryWrapper);
    }

    @Override
    public User getByLoginName(String loginName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        return this.getOne(queryWrapper);
    }

    @Override
    public User addUser(String name, String loginName) {
        User user = new User();
        user.setIsAdmin(2);
        user.setIsDeleted(1);
        user.setIsDcz(2);
        user.setName(name);
        user.setLoginName(loginName);
        user.setPwd(Md5Util.encrypt(defaultPwd));
        this.save(user);
        return user;
    }

    @Override
    public User getByLoginName(String loginName, Long id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName).ne("id",id);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<User> getSimilarNameBy(String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        return list(queryWrapper);
    }

    @Override
    public void batchUpdateUsers(List<User> users) {
        this.updateBatchById(users);
    }
}
